package com.baitengsoft.patentapimonitor.api.controller;

import cn.hutool.core.collection.CollUtil;
import com.baitengsoft.patentapimonitor.api.entity.WebPlatformEntity;
import com.baitengsoft.patentapimonitor.api.entity.WebUserinfoEntity;
import com.baitengsoft.patentapimonitor.api.model.WebMonSummaryExportinfo;
import com.baitengsoft.patentapimonitor.api.model.WebMonSummaryInfo;
import com.baitengsoft.patentapimonitor.api.model.param.SearchMonSummaryParam;
import com.baitengsoft.patentapimonitor.api.service.PacPatentinfoService;
import com.baitengsoft.patentapimonitor.api.service.WebExcelExportService;
import com.baitengsoft.patentapimonitor.api.service.WebPlatformService;
import com.baitengsoft.patentapimonitor.common.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 专利汇总controller
 * @author Chen 2018/1/15
 */
@RestController
@RequestMapping("api/v1/web/monitorsum")
@Api(value = "前台-专利监控-监控汇总",description = "监控汇总")
public class WebMonSummaryController extends AbstractController {
    @Autowired
    private PacPatentinfoService pacPatentinfoService;
    @Autowired
    private WebPlatformService webPlatformService;
    @Autowired
    private WebExcelExportService webExcelExportService;

    /**
     * 获取汇总
     */
    @ApiOperation(value ="汇总信息")
    @GetMapping("/suminfo")
    @ResponseBody
    public Res getMonSummaryInfo(){
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("登录信息失效");
        }
        //获取届满统计时间
        WebPlatformEntity webPlatformEntity = webPlatformService.queryById(userInfo.getPlatformid());
        return pacPatentinfoService.getMonSummaryinfo(webPlatformEntity);
    }


    /**
     * 获取专利汇总列表
     */
    @ApiOperation(value ="获取专利汇总列表")
    @GetMapping("/list")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sort", value = "排序列", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "order", value = "排序方式[desc asc]", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "areaId", value = "区域ID", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "applicantName", value = "权利人名字", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "type", name = "type", value = "1,企业，2个人，3，院校，4科研机构，5机关团体，6其他", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageNumber", value = "页号", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页多少条", required = true)
    })
    public Res getMonSummaryList(@ApiIgnore SearchMonSummaryParam param){
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("登录信息失效");
        }
        //获取届满统计时间
        PageResult<WebMonSummaryInfo> result = pacPatentinfoService.queryList(param, userInfo.getPlatformid());
        return Res.ok().put("list",result);
    }


    @ApiOperation(value ="导出")
    @GetMapping("/listexport")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sort", value = "排序列", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "order", value = "排序方式[desc asc]", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "areaId", value = "区域ID", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "applicantName", value = "权利人名字", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "type", name = "type", value = "1,企业，2个人，3，院校，4科研机构，5机关团体，6其他", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageNumber", value = "页号", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页多少条", required = true)
    })
    public Res listexport(@ApiIgnore SearchMonSummaryParam param, HttpServletResponse response){
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("登录信息失效");
        }
        //获取届满统计时间
        WebPlatformEntity webPlatformEntity = webPlatformService.queryById(userInfo.getPlatformid());
        WebMonSummaryExportinfo sumInfo = pacPatentinfoService.getMonSummaryExportinfo(webPlatformEntity);//汇总信息
        PageResult<WebMonSummaryInfo> result = pacPatentinfoService.queryList(param, userInfo.getPlatformid());
        if (result == null || result.getItems() == null || result.getItems().size() < 0) return Res.error("无数据");
        List<WebMonSummaryInfo> infos = result.getItems();
        Map<String, String> mapExcel = new HashMap<>();
        mapExcel.put("title", "监控汇总");
        mapExcel.put("months", "将来"+sumInfo.getMonths()+"个月即将届满");
        mapExcel.put("count",sumInfo.getCount()+"");
        mapExcel.put("inInfo", sumInfo.getInInfo()+"");
        mapExcel.put("outInfo",sumInfo.getOutInfo()+"");
        mapExcel.put("expiryInfo", sumInfo.getExpiryInfo()+"");
        return WebExcelUtils.exportSuminfoExcelFile(infos,response,"监控汇总.xls",mapExcel);
    }

}
